package com.guigu.youxuan.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.mapper.BackUserMapper;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.dto.LoginForm;
import com.guigu.youxuan.service.IBackUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class BackUserServiceImpl extends ServiceImpl<BackUserMapper, BackUser> implements IBackUserService {

    @Override
    public BackUser getBackUser(LoginForm loginForm) {
        QueryWrapper<BackUser> wrapper = new QueryWrapper<>();
        //账号密码
        if(loginForm != null) {
            if(!StringUtils.isEmpty(loginForm.getUsername())) {
                wrapper.eq("username",loginForm.getUsername());
            }
            if(!StringUtils.isEmpty(loginForm.getPassword())) {
                wrapper.eq("password",loginForm.getPassword());
            }
        }
        //状态标志0 删除标志0
        wrapper.eq("status",0);
        wrapper.eq("is_deleted",0);

        BackUser backUser = this.getOne(wrapper);

        return backUser;
    }

    @Override
    public PageUtils<BackUser> getBackUserList(BackUser backUser, int currentPage, int pageSize) {
        QueryWrapper<BackUser> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if(backUser != null) {
           if(!StringUtils.isEmpty(backUser.getUsername())) {
               wrapper.like("username",backUser.getUsername());
           }
           if(!StringUtils.isEmpty(backUser.getPhone())) {
               wrapper.eq("phone",backUser.getPhone());
           }
           if(!StringUtils.isEmpty(backUser.getName())) {
               wrapper.like("name",backUser.getName());
           }
        }

        PageHelper.startPage(currentPage,pageSize);
        PageHelper.orderBy("id desc");
        List<BackUser> list = this.list(wrapper);
        PageInfo<BackUser> pageInfo = new PageInfo<>(list);

        PageUtils<BackUser> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }

    @Override
    public void switchStatus(BackUser backUser) {
        if(backUser != null){
            this.updateById(backUser);
        }
    }

    @Override
    public void addBackUser(BackUser backUser) {
        if(backUser != null)
            this.save(backUser);
    }

    @Override
    public BackUser queryBackUser(Long userId) {
            BackUser backUser = this.getById(userId);
            return backUser;
    }

    @Override
    public void updateBackUser(BackUser backUser) {
        if(backUser != null)
             this.updateById(backUser);
    }

    @Override
    public void delBackUser(BackUser backUser) {
        if(backUser != null)
            backUser.setIsDeleted((byte)1);
            this.updateById(backUser);
    }

    @Override
    public void delsBackUser(List<BackUser> backUserList) {
        for(BackUser backUser : backUserList) {
            backUser.setIsDeleted((byte)1);
            this.updateById(backUser);
        }
    }
}
